﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;

public partial class Controls_Template_SearchCustom : System.Web.UI.UserControl
{
    class_database db;
    DataTable resultTable;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindListBoxPhamVi();
    }

    protected void BindListBoxPhamVi()
    {
        listBoxPhamVi.Items.Clear();
        listBoxPhamVi.Items.Add(new ListItem("All", "0"));
        string[] text = { "Thông tin chung", "Thông tin kỹ thuật chi tiết", "Thông tin file", "Thông tin hàm diệt" };
        string[] value = { "1", "2", "3", "4" };
        for (int i = 0; i < 4; i++)
        {
            ListItem item = new ListItem(text[i], value[i]);
            item.Attributes.Add("style", "margin-left: 40px");
            listBoxPhamVi.Items.Add(item);
        }
        //string sql = "SELECT * FROM tbl_loaimau";
        //db = new class_database();
        //DataTable dt = db.GetDataTable(sql);
        //if (dt != null && dt.Rows.Count > 0)
        //{
        //    string prefix = "0 ";
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        string value = prefix + dt.Rows[i]["id"].ToString();
        //        string text = "   " + dt.Rows[i]["name"].ToString();
        //        ListItem item = new ListItem(text, value);
        //        item.Attributes.Add("style", "margin-left: 40px");
        //        listBoxPhamVi.Items.Add(item);
        //        BindListBoxMau(dt.Rows[i]["id"].ToString(), value);
        //    }
        //}
    }

    //protected void BindListBoxMau(string idLoaiMau, string valueParentListItem)
    //{
    //    string sql = "SELECT id, name FROM tbl_mau WHERE type = " + idLoaiMau;
    //    db = new class_database();
    //    DataTable dt = db.GetDataTable(sql);
    //    if (dt != null && dt.Rows.Count > 0)
    //    {
    //        string prefix = valueParentListItem + " ";
    //        for (int i = 0; i < dt.Rows.Count; i++)
    //        {
    //            string value = prefix + dt.Rows[i]["id"].ToString();
    //            string text = dt.Rows[i]["name"].ToString();
    //            ListItem item = new ListItem(text, value);
    //            item.Attributes.Add("style", "margin-left: 80px");
    //            listBoxPhamVi.Items.Add(item);
    //            BindListBoxBaiViet(dt.Rows[i]["id"].ToString(), value);
    //        }
    //    }
    //}

    //protected void BindListBoxBaiViet(string idMau, string valueParentListItem)
    //{
    //    string sql = "SELECT tbl_baiphantich.idloaibai as LoaiBai, tbl_loaibaiphantich.name, tbl_baiphantich.id"
    //        + " FROM tbl_baiphantich, tbl_loaibaiphantich"
    //        + " WHERE tbl_baiphantich.idloaibai = tbl_loaibaiphantich.id"
    //        + " AND tbl_baiphantich.idmau = " + idMau;
    //    db = new class_database();
    //    DataTable dt = db.GetDataTable(sql);
    //    if (dt != null && dt.Rows.Count > 0)
    //    {
    //        string prefix = valueParentListItem + " ";
    //        for (int i = 0; i < dt.Rows.Count; i++)
    //        {
    //            if (dt.Rows[i]["LoaiBai"].ToString() != "5")
    //            {
    //                string value = prefix + dt.Rows[i]["id"].ToString();
    //                string text = dt.Rows[i]["name"].ToString();
    //                ListItem item = new ListItem(text, value);
    //                item.Attributes.Add("style", "margin-left: 120px");
    //                listBoxPhamVi.Items.Add(item);
    //            }
    //        }
    //    }
    //}

    private void SetTitleCustomSearch(string value)
    {
        if (value == "0")
        {
            lblTitleCustomSearch.Text = "Phạm vi: <u>toàn bộ</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
        }
        else if (value == "1")
        {
            lblTitleCustomSearch.Text = "Phạm vi: <u>thông tin chung</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
        }
        else if (value == "2")
        {
            lblTitleCustomSearch.Text = "Phạm vi: <u>thông tin kỹ thuật chi tiết</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
        }
        else if (value == "3")
        {
            lblTitleCustomSearch.Text = "Phạm vi: <u>thông tin file</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
        }
        else if (value == "4")
        {
            lblTitleCustomSearch.Text = "Phạm vi: <u>thông tin hàm diệt</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
        }
    }

    //private void SetTitleForCustomSearch(int numberSpace, string stringPhamVi)
    //{
    //    if (numberSpace == 0)
    //    {
    //        lblTitleCustomSearch.Text = "Phạm vi: <u>toàn bộ</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
    //    }
    //    else if (numberSpace == 1)
    //    {
    //        string loaiMau = listBoxPhamVi.SelectedItem.Text;
    //        lblTitleCustomSearch.Text = "Phạm vi: <u>" + loaiMau + "</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
    //    }
    //    else if (numberSpace == 2)
    //    {
    //        string tenMau = listBoxPhamVi.SelectedItem.Text;
    //        string sql = "SELECT tbl_loaimau.name FROM tbl_mau, tbl_loaimau WHERE tbl_mau.type = tbl_loaimau.id"
    //            + " AND tbl_mau.name LIKE '" + tenMau + "'";
    //        db = new class_database();
    //        DataTable dt = db.GetDataTable(sql);
    //        if (dt != null && dt.Rows.Count > 0)
    //        {
    //            string loaiMau = dt.Rows[0]["name"].ToString();
    //            lblTitleCustomSearch.Text = "Phạm vi: <u>" + loaiMau + "</u> - " + "<u>" + tenMau + "</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
    //        }
    //    }
    //    else if (numberSpace == 3)
    //    {
    //        string baiViet = listBoxPhamVi.SelectedItem.Text;
    //        int positionFinalSpace = GetPostionFinalSpace(stringPhamVi);
    //        string idBaiViet = stringPhamVi.Substring(positionFinalSpace + 1, stringPhamVi.Length - 1 - positionFinalSpace);
    //        string idMau = GetIDMauFromIDBaiViet(idBaiViet);
    //        string sql = "SELECT tbl_mau.name as NameMau, tbl_loaimau.name as NameLoaiMau FROM tbl_loaimau, tbl_mau"
    //            + " WHERE tbl_mau.type = tbl_loaimau.id AND tbl_mau.id = " + idMau;
    //        db = new class_database();
    //        DataTable dt = db.GetDataTable(sql);
    //        string tenMau = "";
    //        string tenLoaiMau = "";
    //        if (dt != null && dt.Rows.Count > 0)
    //        {
    //            tenMau = dt.Rows[0]["NameMau"].ToString();
    //            tenLoaiMau = dt.Rows[0]["NameLoaiMau"].ToString();
    //        }
    //        lblTitleCustomSearch.Text = "Phạm vi: <u>" + tenLoaiMau + "</u> - <u>" + tenMau + "</u> - <u>" + baiViet + "</u>. Từ khóa: <u>" + tbKeySearch.Text + "</u>";
    //    }
    //}

    private string GetIDLoaiMauFromIDMau(string idMau)
    {
        string idLoaiMau = "0";
        string sql = "SELECT type FROM tbl_mau WHERE id = " + idMau;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            idLoaiMau = dt.Rows[0]["type"].ToString();
        }
        return idLoaiMau;
    }

    private string GetIDMauFromIDBaiViet(string idBaiViet)
    {
        string idMau = "0";
        string sql = "SELECT idmau FROM tbl_baiphantich WHERE id = " + idBaiViet;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            idMau = dt.Rows[0]["idmau"].ToString();
        }
        return idMau;
    }

    private int GetPostionFinalSpace(string stringInput)
    {
        int result = 0;
        int lengStringInput = stringInput.Length;
        for (int i = lengStringInput - 1; i >= 0; i--)
        {
            if (stringInput[i].ToString() == " ")
            {
                result = i;
                break;
            }
        }
        return result;
    }

    private int CountSpaceChars(string stringPhamVi)
    {
        int result = 0;
        foreach (char c in stringPhamVi)
        {
            if (char.IsWhiteSpace(c))
            {
                result++;
            }
        }
        return result;
    }

    protected void DisplayResult(DataTable dt)
    {
        searchOption.Attributes.Add("style", "display: none");
    }

    protected DataTable Search(int numberSpace, string keySearch)
    {
        DataTable dt = new DataTable();
        if (numberSpace == 0)
        {

        }
        return dt;
    }

    private DataTable SearchAll(string keySearch)
    {
        DataTable dt = new DataTable();

        return dt;
    }

    private DataTable SearchMau(string keySearch, string idMau)
    {
        DataTable dt = new DataTable();

        return dt;
    }

    private bool SearchBaiViet(string keySearch, string loaiBaiViet, string idMau)
    {
        bool check = false;
        db = new class_database();
        if (loaiBaiViet == "1")
        {
            //Tìm trong tbl_ttc_detail
            string sqlCheck1 = "SELECT * FROM tbl_ttc_detail WHERE MATCH(noidung, thongtin) AGAINST ('" + keySearch + "') AND idmau = " + idMau;
            string sqlCheck2 = "SELECT * FROM tbl_ttc_detail WHERE MATCH(noidungdecode, thongtindecode) AGAINST ('" + keySearch + "') AND idmau = " + idMau;
            DataTable dt1 = db.GetDataTable(sqlCheck1);
            DataTable dt2 = db.GetDataTable(sqlCheck2);
            if ((dt1 != null && dt1.Rows.Count > 0) || (dt2 != null && dt2.Rows.Count > 0))
            {
                check = true;
            }
            return check;
        }
        else if (loaiBaiViet == "2")
        {
            //Tìm trong tbl_ttkt_detail
            string sqlCheck = "SELECT * FROM tbl_ttkt_detail WHERE MATCH(noidungdecode) AGAINST ('" + keySearch + "') AND idmau = " + idMau;
            DataTable dt = db.GetDataTable(sqlCheck);
            if (dt != null && dt.Rows.Count > 0)
            {
                check = true;
            }
            return check;
        }
        else if (loaiBaiViet == "3")
        {
            //Tìm trong tbl_ttf_detail
            string sqlCheck = "SELECT * FROM tbl_ttf_detail WHERE MATCH(noidung, thongtin) AGAINST ('" + keySearch + "') AND idmau = " + idMau;
            DataTable dt = db.GetDataTable(sqlCheck);
            if (dt != null && dt.Rows.Count > 0)
            {
                check = true;
            }
            return check;
        }
        else if (loaiBaiViet == "4")
        {
            //Tìm trong tbl_tthd_detail
            string sqlCheck = "SELECT * FROM tbl_tthd_detail WHERE MATCH(noidung, thongtin) AGAINST ('" + keySearch + "') AND idmau = " + idMau;
            DataTable dt = db.GetDataTable(sqlCheck);
            if (dt != null && dt.Rows.Count > 0)
            {
                check = true;
            }
            return check;
        }
        return check;
    }

    private List<string> GetListIDLoaiBaiVietFromIDMau(string idMau)
    {
        db = new class_database();
        List<string> listIDBaiViet = new List<string> { };
        string sql = "SELECT idloaibai FROM tbl_baiphantich WHERE idmau = " + idMau;
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string idLoaiBai = dt.Rows[i]["idloaibai"].ToString();
                if (idLoaiBai != "5")
                {
                    listIDBaiViet.Add(idLoaiBai);
                }
            }
        }
        return listIDBaiViet;
    }

    private string GetNameLink(string idLoaiBaiViet)
    {
        string nameLink = "";
        if (idLoaiBaiViet == "1")
        {
            nameLink = "ThongTinChung";
        }
        else if (idLoaiBaiViet == "2")
        {
            nameLink = "ThongTinKyThuatChiTiet";
        }
        else if (idLoaiBaiViet == "3")
        {
            nameLink = "ThongTinFile";
        }
        else if (idLoaiBaiViet == "4")
        {
            nameLink = "ThongTinHamDiet";
        }
        return nameLink;
    }

    private string GetIDBaiViet(string idLoaiBai, string idMau)
    {
        string idBaiViet = "";
        string sql = "SELECT id FROM tbl_baiphantich WHERE idloaibai = " + idLoaiBai + " AND idmau = " + idMau;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            idBaiViet = dt.Rows[0]["id"].ToString();
        }
        return idBaiViet;
    }

    private List<string> GetListIDBaiViet(string valueSelectListBox, string keySearch) 
    {
        SetTitleCustomSearch(valueSelectListBox);
        //string conditionPublic = "permission = 0"
        db = new class_database();
        List<string> listIDBaiViet = new List<string>();
        List<string> listIDMau = new List<string>();
        // tìm toàn bộ
        if (valueSelectListBox == "0")
        {

        }
        // tìm trong thông tin chung
        else if (valueSelectListBox == "1")
        {
            string sqlCheck1 = "SELECT DISTINCT idmau FROM tbl_ttc_detail WHERE MATCH(noidung, thongtin) AGAINST ('" + keySearch + "')";
            string sqlCheck2 = "SELECT DISTINCT idmau FROM tbl_ttc_detail WHERE MATCH(noidungdecode, thongtindecode) AGAINST ('" + keySearch + "')";
            DataTable dt1 = db.GetDataTable(sqlCheck1);
            DataTable dt2 = db.GetDataTable(sqlCheck2);
            if ((dt1 != null && dt1.Rows.Count > 0) || (dt2 != null && dt2.Rows.Count > 0))
            {
                List<string> originalListIDMau = new List<string>();
                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        originalListIDMau.Add(dt1.Rows[i]["idmau"].ToString());
                    }
                }
                if (dt2.Rows.Count > 0)
                {
                    for (int i = 0; i < dt2.Rows.Count; i++)
                    {
                        originalListIDMau.Add(dt2.Rows[i]["idmau"].ToString());
                    }
                }
                listIDMau = originalListIDMau.Distinct().ToList();
            }
            if (listIDMau.Count > 0)
            {
                for (int i = 0; i < listIDMau.Count; i++)
                {
                    string sql = "SELECT id FROM tbl_baiphantich WHERE idloaibai = 1 AND idmau = " + listIDMau[i];
                    DataTable dt = db.GetDataTable(sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        listIDBaiViet.Add(dt.Rows[0]["id"].ToString());
                    }
                }
            }
        }
        // tìm trong thông tin kỹ thuật chi tiết
        else if (valueSelectListBox == "2")
        {
            string sqlCheck = "SELECT DISTINCT idmau FROM tbl_ttkt_detail WHERE MATCH(noidungdecode) AGAINST ('" + keySearch + "')";
            DataTable dt = db.GetDataTable(sqlCheck);
            if (dt != null && dt.Rows.Count > 0)
            {
                listIDMau.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listIDMau.Add(dt.Rows[i]["idmau"].ToString());
                }

            }
            if (listIDMau.Count > 0)
            {
                for (int i = 0; i < listIDMau.Count; i++)
                {
                    string sql = "SELECT id FROM tbl_baiphantich WHERE idloaibai = 2 AND idmau = " + listIDMau[i];
                    DataTable dtIDBaiViet = db.GetDataTable(sql);
                    if (dtIDBaiViet != null && dtIDBaiViet.Rows.Count > 0)
                    {
                        listIDBaiViet.Add(dtIDBaiViet.Rows[0]["id"].ToString());
                    }
                }
            }
        }
        // tìm trong thông tin file
        else if (valueSelectListBox == "3")
        {
            string sqlCheck = "SELECT DISTINCT idmau FROM tbl_ttf_detail WHERE MATCH(noidung, thongtin) AGAINST ('" + keySearch + "')";
            DataTable dt = db.GetDataTable(sqlCheck);
            if (dt != null && dt.Rows.Count > 0)
            {
                listIDMau.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listIDMau.Add(dt.Rows[i]["idmau"].ToString());
                }

            }
            if (listIDMau.Count > 0)
            {
                for (int i = 0; i < listIDMau.Count; i++)
                {
                    string sql = "SELECT id FROM tbl_baiphantich WHERE idloaibai = 3 AND idmau = " + listIDMau[i];
                    DataTable dtIDBaiViet = db.GetDataTable(sql);
                    if (dtIDBaiViet != null && dtIDBaiViet.Rows.Count > 0)
                    {
                        listIDBaiViet.Add(dtIDBaiViet.Rows[0]["id"].ToString());
                    }
                }
            }
        }
        // tìm trong thông tin hàm diệt
        else if (valueSelectListBox == "4")
        {
            string sqlCheck = "SELECT DISTINCT idmau FROM tbl_tthd_detail WHERE MATCH(noidung, thongtin) AGAINST ('" + keySearch + "')";
            DataTable dt = db.GetDataTable(sqlCheck);
            if (dt != null && dt.Rows.Count > 0)
            {
                listIDMau.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listIDMau.Add(dt.Rows[i]["idmau"].ToString());
                }

            }
            if (listIDMau.Count > 0)
            {
                for (int i = 0; i < listIDMau.Count; i++)
                {
                    string sql = "SELECT id FROM tbl_baiphantich WHERE idloaibai = 4 AND idmau = " + listIDMau[i];
                    DataTable dtIDBaiViet = db.GetDataTable(sql);
                    if (dtIDBaiViet != null && dtIDBaiViet.Rows.Count > 0)
                    {
                        listIDBaiViet.Add(dtIDBaiViet.Rows[0]["id"].ToString());
                    }
                }
            }
        }


        return listIDBaiViet;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string keySearch = "";
        keySearch = tbKeySearch.Text;
        if (keySearch == "" || keySearch.Length < 4)
        {

        }
        else
        {
            string valueSelectListBox = listBoxPhamVi.SelectedValue.ToString();
            List<string> listIDBaiViet = GetListIDBaiViet(valueSelectListBox, keySearch);
            string sql = "SELECT DISTINCT idmau FROM tbl_baiphantich WHERE id IN (";
            for (int i = 0; i < listIDBaiViet.Count; i++)
            {
                if (i == listIDBaiViet.Count - 1)
                {
                    sql += listIDBaiViet[i] + ")";
                }
                else
                {
                    sql += listIDBaiViet[i] + ", ";
                }
            }
            DataTable dtGetListIDMau = db.GetDataTable(sql);
            if (dtGetListIDMau != null && dtGetListIDMau.Rows.Count > 0)
            {
                string sqlGetMau = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
                " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
                " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
                " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
                " WHERE tbl_mau.type = tbl_loaimau.id" +
                " AND tbl_mau.nguoiphantich = tbl_user.id" +
                " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
                " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
                " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
                " AND tbl_mau.id IN (";
                for (int i = 0; i < dtGetListIDMau.Rows.Count; i++)
                {
                    if (i == dtGetListIDMau.Rows.Count - 1)
                    {
                        sqlGetMau += dtGetListIDMau.Rows[i]["idmau"].ToString() + ")";
                    }
                    else
                    {
                        sqlGetMau += dtGetListIDMau.Rows[i]["idmau"].ToString() + ", ";
                    }
                }
                DataTable dtGetListMau = db.GetDataTable(sqlGetMau);
                if (dtGetListMau != null && dtGetListMau.Rows.Count > 0)
                {
                    rptResultSearch.DataSource = dtGetListMau;
                    rptResultSearch.DataBind();
                    string nameLink = GetNameLink(valueSelectListBox);
                    for (int i = 0; i < rptResultSearch.Items.Count; i++)
                    {
                        HyperLink hl = rptResultSearch.Items[i].FindControl(nameLink) as HyperLink;
                        Button button = rptResultSearch.Items[i].FindControl("btnEdit") as Button;
                        string idMau = button.CommandArgument.ToString();
                        hl.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=" + nameLink + "&idMau=" + idMau;
                        hl.Attributes.Add("style", "display: block");
                    }
                }
            }

             

            //string stringPhamVi = listBoxPhamVi.SelectedValue.ToString();
            //int numberSpace = CountSpaceChars(stringPhamVi);
            //SetTitleForCustomSearch(numberSpace, stringPhamVi);
            //if (numberSpace == 3)
            //{
            //    // Tìm cụ thể từng bài
            //    string baiViet = listBoxPhamVi.SelectedItem.Text;
            //    int positionFinalSpace = GetPostionFinalSpace(stringPhamVi);
            //    string idBaiViet = stringPhamVi.Substring(positionFinalSpace + 1, stringPhamVi.Length - 1 - positionFinalSpace);
            //    string idMau = GetIDMauFromIDBaiViet(idBaiViet);
            //    string sqlGetLoaiBaiViet = "SELECT idloaibai FROM tbl_baiphantich WHERE id = " + idBaiViet;
            //    db = new class_database();
            //    DataTable dt = db.GetDataTable(sqlGetLoaiBaiViet);
            //    string idLoaiBaiViet = "0";
            //    if (dt != null && dt.Rows.Count > 0)
            //    {
            //        idLoaiBaiViet = dt.Rows[0]["idloaibai"].ToString();
            //    }
            //    bool checkBaiViet = SearchBaiViet(keySearch, idLoaiBaiViet, idMau);
            //    if (checkBaiViet == true)
            //    {
            //string sqlGetMau = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            //" tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            //" tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            //" FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            //" WHERE tbl_mau.type = tbl_loaimau.id" +
            //" AND tbl_mau.nguoiphantich = tbl_user.id" +
            //" AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            //" AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            //" AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            //" AND tbl_mau.id = " + idMau;
            //        DataTable dtMau = db.GetDataTable(sqlGetMau);
            //        rptResultSearch.DataSource = dtMau;
            //        rptResultSearch.DataBind();
            //string nameLink = GetNameLink(idLoaiBaiViet);
            //for (int i = 0; i < rptResultSearch.Items.Count; i++)
            //{
            //    HyperLink hl = rptResultSearch.Items[i].FindControl(nameLink) as HyperLink;
            //    hl.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=" + nameLink + "&idMau=" + idMau;
            //    hl.Attributes.Add("style", "display: block");
            //}
            //    }
            //    else
            //    {

            //    }
            //}
            //else if (numberSpace == 2)
            //{
            //    // Tìm trong mẫu
            //    int positionFinalSpace = GetPostionFinalSpace(stringPhamVi);
            //    string idMau = stringPhamVi.Substring(positionFinalSpace + 1, stringPhamVi.Length - 1 - positionFinalSpace);
            //    List<string> listIDLoaiBaiViet = GetListIDLoaiBaiVietFromIDMau(idMau);
            //    string sqlGetMau = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            //        " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            //        " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            //        " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            //        " WHERE tbl_mau.type = tbl_loaimau.id" +
            //        " AND tbl_mau.nguoiphantich = tbl_user.id" +
            //        " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            //        " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            //        " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            //        " AND tbl_mau.id = " + idMau;
            //    db = new class_database();
            //    DataTable dtMau = db.GetDataTable(sqlGetMau);
            //    rptResultSearch.DataSource = dtMau;
            //    rptResultSearch.DataBind();
            //    for (int i = 0; i < listIDLoaiBaiViet.Count; i++)
            //    {
            //        string idLoaiBaiViet = listIDLoaiBaiViet[i].ToString();
            //        bool checkBaiViet = SearchBaiViet(keySearch, idLoaiBaiViet, idMau);
            //        if (checkBaiViet == true)
            //        {
            //            string nameLink = GetNameLink(idLoaiBaiViet);
            //            HyperLink hl = rptResultSearch.Items[0].FindControl(nameLink) as HyperLink;
            //            hl.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=" + nameLink + "&idMau=" + idMau;
            //            hl.Attributes.Add("style", "display: block");
            //        }
            //    }
            //}
            //else if (numberSpace == 1)
            //{
            //    // Tìm trong loại mẫu
            //    int positionFinalSpace = GetPostionFinalSpace(stringPhamVi);
            //    string idLoaiMau = stringPhamVi.Substring(positionFinalSpace + 1, stringPhamVi.Length - 1 - positionFinalSpace);
            //    List<string> listIDMau = GetIDMauFromIDLoaiMau(idLoaiMau);
            //    string sqlGetAllMau = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            //        " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            //        " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            //        " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            //        " WHERE tbl_mau.type = tbl_loaimau.id" +
            //        " AND tbl_mau.nguoiphantich = tbl_user.id" +
            //        " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            //        " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            //        " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            //        " AND tbl_mau.type = " + idLoaiMau;
            //    db = new class_database();
            //    DataTable dt = db.GetDataTable(sqlGetAllMau);
            //    if (dt != null && dt.Rows.Count > 0)
            //    {
            //        rptResultSearch.DataSource = dt;
            //        rptResultSearch.DataBind();
            //        for (int i = 0; i < dt.Rows.Count; i++)
            //        {
            //            bool checkBindRepeater = false;
            //            string idMau = dt.Rows[i]["id"].ToString();
            //            List<string> listIDLoaiBaiViet = GetListIDLoaiBaiVietFromIDMau(idMau);
            //            for (int j = 0; j < listIDLoaiBaiViet.Count; j++)
            //            {
            //                string idLoaiBaiViet = listIDLoaiBaiViet[j];
            //                bool checkBaiViet = SearchBaiViet(keySearch, idLoaiBaiViet, idMau);
            //                if (checkBaiViet == true)
            //                {
            //                    checkBindRepeater = true;
            //                    string nameLink = GetNameLink(idLoaiBaiViet);
            //                    HyperLink hl = rptResultSearch.Items[i].FindControl(nameLink) as HyperLink;
            //                    hl.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=" + nameLink + "&idMau=" + idMau;
            //                    hl.Attributes.Add("style", "display: block");
            //                }
            //            }
            //            if (checkBindRepeater == false)
            //            {
            //                rptResultSearch.Items[i].Visible = false;
            //            }
            //        }
            //    }

            //}
            //else if (numberSpace == 0)
            //{
            //    // Tìm trong toàn bộ
            //    string sqlGetAllMau = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            //        " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            //        " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            //        " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            //        " WHERE tbl_mau.type = tbl_loaimau.id" +
            //        " AND tbl_mau.nguoiphantich = tbl_user.id" +
            //        " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            //        " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            //        " AND tbl_mau.kithuatanti = tbl_kithuatanti.id";
            //    db = new class_database();
            //    DataTable dt = db.GetDataTable(sqlGetAllMau);
            //    if (dt != null && dt.Rows.Count > 0)
            //    {
            //        rptResultSearch.DataSource = dt;
            //        rptResultSearch.DataBind();
            //        for (int i = 0; i < dt.Rows.Count; i++)
            //        {
            //            bool checkBindRepeater = false;
            //            string idMau = dt.Rows[i]["id"].ToString();
            //            List<string> listIDLoaiBaiViet = GetListIDLoaiBaiVietFromIDMau(idMau);
            //            for (int j = 0; j < listIDLoaiBaiViet.Count; j++)
            //            {
            //                string idLoaiBaiViet = listIDLoaiBaiViet[j];
            //                bool checkBaiViet = SearchBaiViet(keySearch, idLoaiBaiViet, idMau);
            //                if (checkBaiViet == true)
            //                {
            //                    checkBindRepeater = true;
            //                    string nameLink = GetNameLink(idLoaiBaiViet);
            //                    HyperLink hl = rptResultSearch.Items[i].FindControl(nameLink) as HyperLink;
            //                    hl.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=" + nameLink + "&idMau=" + idMau;
            //                    hl.Attributes.Add("style", "display: block");
            //                }
            //            }
            //            if (checkBindRepeater == false)
            //            {
            //                rptResultSearch.Items[i].Visible = false;
            //            }
            //        }
            //    }
            //}
            Session["keySearch"] = keySearch;
            
            searchOption.Attributes.Add("style", "display: none");
            searchResult.Attributes.Add("style", "display: block");
        }
    }

    private List<string> GetIDMauFromIDLoaiMau(string idLoaiMau)
    {
        List<string> listIDMau = new List<string> { };
        db = new class_database();
        string sql = "SELECT id FROM tbl_mau WHERE type = " + idLoaiMau;
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                listIDMau.Add(dt.Rows[i]["id"].ToString());
            }
        }
        return listIDMau;
    }

}

